// pages/map/orderAndDriverGoing/orderAndDriverGoing.js
const handler = require("../../../utils/function.js");
var api = require("../../../api.js"),
    app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        //用户当前位置图标
        polyline: [{
            points: [{
                latitude: 34.780254,
                longitude: 113.699559
            }, {
                longitude: 113.701855,
                latitude: 34.779778
            }],
            color: "#FF0000DD",
            width: 2,
            dottedLine: true
        }],
        controls: [{
            id: 1,
            iconPath: '../../../images/user/icon-22.png',
            position: {
                left: 10,
                top: 10,
                width: 50,
                height: 50
            },
            clickable: true
        }], //人物图片定位
        startAddress: {},
        order_going_m: 0,
        order_going_s: 0,
        myShow: true, //显示my的页面
        zuoyou: "icon-zuoyou", //my
        avatar: "../../../images/user/icon-07.png",
        buttonText: "登录",
        username: "未登录",
        orderGoing: false,
        homeHandlerShow: true,
        phone: "",
        order_no: '',
        is_send: 0,
        left_width: 750
    },
  relstiveServerHandler:function(){
    handler.callPhoneHandler(wx.getStorageSync("serverphone"));
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            order_no: options.order_no,
        })
        //this.time()
    },
    regionchange(e) {
        // console.log("regionchange", e.type)
    },
    markertap(e) {
        // console.log("markertap", e.markerId)
    },
    // 地图图片点击事件
    controltap(e) {
        // console.log("map controltap", e.controlId)
        if (e.controlId === 1) {
            this.openMyPageHandler();
        } else {
            const addresses = handler.getLatLntHandler((res) => {
                //  console.log("一键定位",res);
                this.setData({
                    startAddress: res.startAddress, //开始位置
                })
            });
        }
    },
    // openMyPageHandler 打开我的页面
    openMyPageHandler: function() {
        const {
            pageRight,
            myShow
        } = this.data;
        // console.log("openMyPageHandler", pageRight, myShow);
        if (myShow) {
            this.setData({
                pageRight: 620,
                left_width: 130,
                myShow: !myShow,
                controls: [{
                    id: 1,
                    iconPath: '../../../images/user/icon-33.png',
                    position: {
                        top: 144,
                        left: 10,
                        width: 41,
                        height: 41
                    },
                    clickable: true
                }]
            });
        } else {
            this.setData({
                pageRight: 0,
                left_width: 750,
                myShow: !myShow,
                controls: [{
                    id: 1,
                    iconPath: '../../../images/user/icon-22.png',
                    position: {
                        left: 10,
                        top: 10,
                        width: 50,
                        height: 50
                    },
                    clickable: true
                }, {
                    id: 2,
                    iconPath: '../../../images/user/icon-30.png',
                    position: {
                        top: 300,
                        left: 10,
                        width: 50,
                        height: 50
                    },
                    clickable: true
                }, ],
            });
            this.changeIcon()
        }
    },
    time: function() {
        const time = setInterval(() => {
            if (this.data.is_send !== 1) {
                this.timeTask();
            } else {
                clearInterval(time)
            }
        }, 1000);
    },
    // 时间定时器
    timeTask: function() {
        let order_going_s = this.data.order_going_s;
        let order_going_m = this.data.order_going_m;
        let is_send = this.data.is_send;
        order_going_s++;
        if (order_going_s > 59) {
            order_going_s = 0;
            order_going_m++;
        }
        this.setData({
            order_going_s,
            order_going_m
        })
    },
    // deleteOrderHandler 取消订单事件
    deleteOrderHandler: function() {
        var that = this;
        app.request({
            url: api.order.cancel_order,
            method: 'post',
            data: {
                order_no: that.data.order_no,
            },
            success: function(rs) {
                if (rs.code == 1) {
                    wx.showToast({
                        title: '订单取消已成功',
                    })
                    clearInterval(this.timeTask)
                    wx.reLaunch({
                      url: '../driverServer/driverServer',
                    })
                    // handler.intoPageHandler('');
                } else {
                    wx.showToast({
                        title: rs.msg,
                    })
                }
            }
        })
    },
    // 用户信息事件
    /**loginHandler用户登录页面 */
    loginHandler: function(e) {
        if (this.data.buttonText === "登录") {
            handler.loginHandler((res) => {
                // console.log("用户信息事件",res);
                this.setData({
                    res,
                });
                const that = this;
                wx.showModal({
                    title: '获取手机号码',
                    content: '我们需要获取你的电话号码，一边与和你保持实时的联系，防止你的订单丢失',
                    success: function(res) {
                        if (res.confirm) {
                            handler.intoPageHandler("../../user/getPhone/getPhone");
                        }
                    },
                    fail: function() {
                        wx.showToast({
                            title: '登录失败',
                            icon: "none"
                        })
                    }
                })
            })
        } else {
            // console.log('退出登录')
            app.request({
                url: api.user.loginout,
                method: 'post',
                data: {},
                success: function(rs) {
                    if (rs.code == 1) {
                        wx.removeStorageSync("userMessage");
                        wx.removeStorageSync("phonep");
                        handler.intoPageHandler('../driverServer/driverServer');
                    }
                    wx.showToast({
                        title: rs.msg,
                        duration: 2000
                    });
                }
            })
            this.setData({
                avatar: "../../../images/user/icon-07.png",
                username: "未登录",
                buttonText: "登录",
                phone: ""
            });
        }
    },
    // intoPageHandler 页面跳转
    intoPageHandler: function(e) {
        // console.log(e.currentTarget.dataset.skip);
        handler.intoPageHandler(e.currentTarget.dataset.skip);
    },
    // callPhoneHandler 联系客服
    callPhoneHandler: function() {
        handler.callPhoneHandler(wx.getStorageSync("serverPhone"));
    },
    // orderGoingHandler 订单进行中事件
    orderGoingHandler: function() {
        wx.showToast({
            title: '您还没有进行中的订单',
            icon: "none"
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        if (wx.getStorageSync("userMessage") !== "") {
            this.setData({
                avatar: wx.getStorageSync("userMessage").avatar,
                username: wx.getStorageSync("userMessage").username,
                buttonText: wx.getStorageSync("userMessage").buttonText,
                phone: wx.getStorageSync("phonep"),
            });
        }
        const time = setInterval(() => {
            if (that.data.is_send == 1) {
                clearInterval(time);
              wx.redirectTo({
                url: '../getOrderNotGoing/getOrderNotGoing?order_no=' + that.data.order_no,
              })
                // wx.navigateTo({
                    
                // })
            } else {
                that.timeTask();
                that.postionTask();
            }
        }, 1000);
        handler.getLatLntHandler(res=>{
          console.log(res,'res11111111111111')
          that.setData({
            startAddress: res.startAddress, //开始位置
          })
        })
        this.changeIcon();
    },
    /*
     * 定时获取订单状态及代驾人员位置
     */
    postionTask: function() {
        let that = this;
        var is_send = that.data.is_send;
        app.request({
            url: api.order.waiting,
            method: 'post',
            data: {
                order_no: that.data.order_no,
            },
            success: function(rs) {
                if (rs.code == 1) {
                    that.setData({
                        is_send: rs.data.is_send,
                        startAddress: rs.data.lang,
                    })
                }
            }
        })
    },
    // 修改用户和一键定位位置
    changeIcon: function() {
        const that = this;
        const {
            controls
        } = this.data;
        wx.getSystemInfo({
            success: function success(res) {
                var controlsH = res.windowWidth / 750 * 350; //计算一像素的宽度 res.windowWidth
                var map_height = res.windowHeight - controlsH;
                // console.log(res, '2222222222')
                controls[1] = {
                    id: 2,
                    iconPath: '../../../images/user/icon-30.png',
                    position: {
                        top: map_height - 56,
                        left: 10,
                        width: 50,
                        height: 50
                    },
                    clickable: true
                };
                that.setData({
                    winPath: {
                        w: res.windowWidth,
                        h: res.windowHeight
                    },
                    map_height,
                    controls,
                });
            }
        });
    },
})